package com.atguigu.gmall.index.config;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.feign.GmallPmsClient;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author Aupt
 * @create 2022-06-01-16:54
 */
@Configuration
public class BloomFilterConfig {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private GmallPmsClient pmsClient;

    public static final String KEY_PREFIX = "index:category:";

    @Bean
    public RBloomFilter bloomFilter() {

        RBloomFilter<Object> bloomFilter = this.redissonClient.getBloomFilter("index:bf");
        bloomFilter.tryInit(1000L, 0.03);
        //向布隆过滤器插入初始化数据
        //调用Pms的远程接口查询数据库中存在的数据
        ResponseVo<List<CategoryEntity>> listResponseVo = this.pmsClient.queryCategoryByParentId(0L);
        List<CategoryEntity> categoryEntities = listResponseVo.getData();
        categoryEntities.forEach(categoryEntity -> {
            bloomFilter.add(KEY_PREFIX + categoryEntity.getId());
        });

        //TODO 广告插入

        return bloomFilter;

    }

}
